package com.ptmall.app.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jauker.widget.BadgeView;
import com.ptmall.app.App;
import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.OrderBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.bean.model.db.Account;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.api.ApiService;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.AddressActivity;
import com.ptmall.app.ui.activity.CollectionActivity;
import com.ptmall.app.ui.activity.CouponActivity;
import com.ptmall.app.ui.activity.GzActivity;
import com.ptmall.app.ui.activity.LoginActivity;
import com.ptmall.app.ui.activity.MessageActivity;
import com.ptmall.app.ui.activity.OrdersActivity;
import com.ptmall.app.ui.activity.SettingActivity;
import com.ptmall.app.ui.activity.ShopApplyActivity;
import com.ptmall.app.ui.activity.ThActivity;
import com.ptmall.app.ui.activity.WalletActivity;
import com.ptmall.app.ui.activity.WebActivity;
import com.ptmall.app.ui.adapter.MeTitleAdapter;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.NoScrollGridView;
import com.ptmall.app.view.PullToRefreshLayout;

import java.util.HashMap;
import java.util.Map;


public class Mefragment extends BaseMvpFragment implements View.OnClickListener {

    View view;

    Button loginforregister;
    TextView loginforphone;

    private Intent intent;

    Account account;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    NoScrollGridView me_itemgridview;

    ImageView setting, headimg;

    LinearLayout order_dzf, order_dps, order_psz, order_sh;

    int[] top_titleIcos = new int[]{
            R.drawable.me_qb,
            R.drawable.me_xxtz,
            R.drawable.me_yhj,
            R.drawable.me_wdsc,
            R.drawable.me_gzjs,
            R.drawable.me_kf,
            R.drawable.me_dzgl,
            R.drawable.me_jrwm,
            R.drawable.me_gyssq,
    };
    String[] top_titleStrs = new String[]{
            "钱包",
            "消息通知",
            "优惠卷",
            "我的收藏",
            "规则介绍",
            "客服",
            "地址管理",
            "加入我们",
            "供应商申请",
    };
    String[] items = new String[]{
            "qianbao",
            "xiaoxi",
            "youhuijuan",
            "shoucang",
            "guize",
            "kefu",
            "dizhi",
            "jiaru",
            "shenqing",
    };

    MeTitleAdapter mMeTitleAdapter;

    RelativeLayout order_all;
    public BadgeView mBadgeView1;
    public BadgeView mBadgeView2;
    public BadgeView mBadgeView3;
    public BadgeView mBadgeView4;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);

        return view;
    }

    @Override
    public void initViews() {
        order_dzf = (LinearLayout) view.findViewById(R.id.order_dzf);
        order_dps = (LinearLayout) view.findViewById(R.id.order_dps);
        order_psz = (LinearLayout) view.findViewById(R.id.order_psz);
        order_sh = (LinearLayout) view.findViewById(R.id.order_sh);
        headimg = (ImageView) view.findViewById(R.id.headimg);
        loginforphone = (TextView) view.findViewById(R.id.loginforphone);
        loginforregister = (Button) view.findViewById(R.id.loginforregister);
        me_itemgridview = (NoScrollGridView) view.findViewById(R.id.me_itemgridview);
        setting = (ImageView) view.findViewById(R.id.setting);
        order_all = (RelativeLayout) view.findViewById(R.id.order_all);
        setting.setOnClickListener(this);

        loginforregister.setOnClickListener(this);

        order_dzf.setOnClickListener(this);
        order_dps.setOnClickListener(this);
        order_psz.setOnClickListener(this);
        order_sh.setOnClickListener(this);
        order_all.setOnClickListener(this);

        mBadgeView1 = new BadgeView(getContext());
        mBadgeView1.setBackground(10, 0xFFFF0000);//色
        mBadgeView1.setTargetView(order_dzf);

        mBadgeView2 = new BadgeView(getContext());
        mBadgeView2.setBackground(10, 0xFFFF0000);//色
        mBadgeView2.setTargetView(order_dps);

        mBadgeView3 = new BadgeView(getContext());
        mBadgeView3.setBackground(10, 0xFFFF0000);//色
        mBadgeView3.setTargetView(order_psz);

        mBadgeView4 = new BadgeView(getContext());
        mBadgeView4.setBackground(10, 0xFFFF0000);//色
        mBadgeView4.setTargetView(order_sh);
    }

    @Override
    public void initEvents() {
        mMeTitleAdapter = new MeTitleAdapter(getContext(), top_titleIcos, top_titleStrs);
        me_itemgridview.setAdapter(mMeTitleAdapter);
        me_itemgridview.setOnItemClickListener(new MyOnItemClickListener());
    }


    @Override
    public void onResume() {
        super.onResume();
        if (isLogined()) {
           getuserinfo();
        } else {
            loginforphone.setVisibility(View.GONE);
            loginforregister.setVisibility(View.VISIBLE);
        }
    }

    public void getuserinfo(){
        apiDataRepository.getUserInfo(null, new ApiNetResponse<Account>() {
            @Override
            public void onSuccess(Account data) {
                account = data;
                GlideUtil.showImg(getContext(), data.image_head, headimg);
                loginforphone.setText(data.nickName+"");
                loginforphone.setVisibility(View.VISIBLE);
                loginforregister.setVisibility(View.GONE);


                mBadgeView1.setVisibility(View.VISIBLE);
                mBadgeView1.setText(data.daifu+"");
                if(data.daifu == 0){
                    mBadgeView1.setVisibility(View.GONE);
                }

                mBadgeView2.setVisibility(View.VISIBLE);
                mBadgeView2.setText(data.daisong+"");
                if(data.daisong == 0){
                    mBadgeView2.setVisibility(View.GONE);
                }

                mBadgeView3.setVisibility(View.VISIBLE);
                mBadgeView3.setText(data.peisong+"");
                if(data.peisong == 0){
                    mBadgeView3.setVisibility(View.GONE);
                }

                mBadgeView4.setVisibility(View.VISIBLE);
                mBadgeView4.setText(data.tui+"");
                if(data.tui == 0){
                    mBadgeView4.setVisibility(View.GONE);
                }

                mMeTitleAdapter.setmsgNum(data.notice_count+"");
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loginforregister:
                intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.setting:
                if (!isLoginedGo()) {
                    return;
                }
                intent = new Intent(getContext(), SettingActivity.class);
                startActivity(intent);
                break;

            case R.id.order_all:

                intent = new Intent(getContext(), OrdersActivity.class);
                startActivity(intent);
                break;
            case R.id.order_dzf:
                intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("position", 1);
                startActivity(intent);
                break;
            case R.id.order_dps:

                intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("position", 2);
                startActivity(intent);
                break;
            case R.id.order_psz:

                intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("position", 3);
                startActivity(intent);
                break;
            case R.id.order_sh:

                intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("position", 4);
                startActivity(intent);
                break;
        }
    }


    private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (items[position]) {
                case "qianbao":
                    if (!isLoginedGo()) {
                        return;
                    }
                    intent = new Intent(getContext(), WalletActivity.class);
                    startActivity(intent);
                    break;
                case "xiaoxi":
                    if (!isLoginedGo()) {
                        return;
                    }
                    intent = new Intent(getContext(), MessageActivity.class);
                    startActivity(intent);
                    break;
                case "youhuijuan":
                    if (!isLoginedGo()) {
                        return;
                    }
                    intent = new Intent(getContext(), CouponActivity.class);
                    startActivity(intent);
                    break;
                case "shoucang":
                    if (!isLoginedGo()) {
                        return;
                    }
                    intent = new Intent(getContext(), CollectionActivity.class);
                    startActivity(intent);
                    break;
                case "dizhi":
                    if (!isLoginedGo()) {
                        return;
                    }
                    intent = new Intent(getContext(), AddressActivity.class);
                    startActivity(intent);
                    break;
                case "shenqing":

                    intent = new Intent(getContext(), ShopApplyActivity.class);
                    startActivity(intent);
                    break;
                case "jiaru":
                    Intent intent = new Intent(getContext(), WebActivity.class);
                    intent.putExtra("title", "加入我们");
                    intent.putExtra("url", HttpMethods.BASE_URL + "Detail/join");
                    startActivity(intent);
                    break;
                case "guize":
                    startActivity(new Intent(getContext(), GzActivity.class));
//                    Intent intent2 = new Intent(getContext(), WebActivity.class);
//                    intent2.putExtra("title","规则介绍");
//                    intent2.putExtra("url", HttpMethods.BASE_URL+"Detail/rule");
//                    startActivity(intent2);
                    break;
                case "kefu":
                    if (!isLoginedGo()) {
                        return;
                    }
                    Intent intent3 = new Intent(getContext(), WebActivity.class);
                    intent3.putExtra("title", "客服");
                    intent3.putExtra("url", account.kefu);
                    startActivity(intent3);
                    break;
            }
        }
    }


}
